package hotel.compare.oversea.controller;

import hotel.base.oversea.constant.SwitchConstants;
import hotel.base.oversea.utils.Result;
import hotel.compare.oversea.entity.switchs.SwitchCreateSubHotel;
import hotel.compare.oversea.entity.switchs.SwitchHotelOnlineSetEntity;
import hotel.compare.oversea.mapper.switchs.SwitchCreateSubHotelMapper;
import hotel.compare.oversea.mapper.switchs.SwitchHotelOnlineSetMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 执行美团涉及定时器相关数据还原或恢复
 * @author qiuyy
 * @date 2020-05-15
 */
@Slf4j
@RestController
@RequestMapping("/dealDataFromJob")
@Api(value = "DealDataFromJob", tags = {"接口"})
public class DealDataFromJobController {
    @Autowired
    SwitchHotelOnlineSetMapper switchHotelOnlineSetMapper;

    @Autowired
    SwitchCreateSubHotelMapper switchCreateSubHotelMapper;

    @Autowired
    RedisTemplate redisTemplate;

    /**
     * 拉取母物理房型和静态房型  参数子酒店在缓存中，该方法重置缓存
     * @param request
     * @return
     */
    @PostMapping("/resetSubHotelIdRedisForJob")
    @ApiOperation(value = "参数子酒店在缓存中重置缓存 只针对配置是id的情况sold=1",notes="必须经过auth授权")
    public Result<Object> resetSaleRoomAboutMtHotelIds(HttpServletRequest request){

        redisTemplate.delete(SwitchConstants.CREATE_SUB_FOR_ROOM_MASTER_JOB);

        redisTemplate.delete(SwitchConstants.CREATE_SUB_FOR_ROOM_STATIC_JOB);
        re(1);
        return new Result<>("成功",0);
    }
    public Result<Object> re(Integer index){
        log.info("参数子酒店在缓存中重置缓存 index={}",index);
        Map findMap = new HashMap();
        findMap.put("index", index);
        findMap.put("pageSize", 1);
        List<SwitchHotelOnlineSetEntity> switchHotelOnlineSetEntityList = switchHotelOnlineSetMapper.queryPageDto(findMap);
        if (null == switchHotelOnlineSetEntityList || switchHotelOnlineSetEntityList.size() == 0) {
            log.info("/////// resetSaleRoomAboutMtHotelIds  无下一城市配置");
            return new Result<>(null,"无下一城市配");//城市设置已无数据 返回data=null
        }else{
            SwitchHotelOnlineSetEntity switchHotelOnlineSetEntity = switchHotelOnlineSetEntityList.get(0);
            Byte sold = switchHotelOnlineSetEntity.getSold();
            if(sold == 1){
                //指定酒店
                if(!StringUtils.isEmpty(switchHotelOnlineSetEntity.getIds())){
                    String[] idsArray = switchHotelOnlineSetEntity.getIds().split(",");
                    if(null != idsArray && idsArray.length>0){
                        for (int i = 0; i <idsArray.length; i++) {
                            String masterId = idsArray[i];
                            log.info("===idsArray["+i+"]={}",idsArray[i]);
                            SwitchCreateSubHotel switchCreateSubHotel = new SwitchCreateSubHotel();
                            switchCreateSubHotel.setMasterHotelId(Integer.valueOf(idsArray[i]));
                            List<SwitchCreateSubHotel> list = switchCreateSubHotelMapper.select(switchCreateSubHotel);
                            if(null != list && list.size()>0){
                                for (SwitchCreateSubHotel sub:list){
                                    log.info("=== i = {} , sub.getSubHotelId()={}",i,sub.getSubHotelId());
                                    redisTemplate.opsForList().leftPush(SwitchConstants.CREATE_SUB_FOR_ROOM_MASTER_JOB,String.valueOf(sub.getSubHotelId()));

                                    redisTemplate.opsForList().leftPush(SwitchConstants.CREATE_SUB_FOR_ROOM_STATIC_JOB,masterId+"_"+String.valueOf(sub.getSubHotelId()));
                                }
                            }else{
                                log.info("/////// i = {} ,  list = 0 ",i);
                            }
                        }
                    }else{
                        log.info("/////// idsArray = 0 switchHotelOnlineSetEntity.getIds()={}",switchHotelOnlineSetEntity.getIds());
                    }
                }
            }else{
                log.info("/////// sold!=1 ,sold={}"+sold);
            }
            return re(index+1);
        }
    }
}
